//
//  RBGFavorisManager.m
//  BateauGenerique
//
//  Created by Florent Champigny on 09/06/2014.
//  Copyright (c) 2014 Kevin De Jesus Ferreira. All rights reserved.
//

#import "RBGFavorisManager.h"

#define FAVORIS_KEY @"favoris"

@implementation RBGFavorisManager

-(id)init
{
    if(self = [super init]){
        [self charger];
    }
    return self;
}

-(void)sauvegarder
{
    NSUserDefaults* preferences = [NSUserDefaults standardUserDefaults];
    
    [preferences setObject:self.favoris forKey:FAVORIS_KEY];
}

-(void)charger
{
    NSUserDefaults* preferences = [NSUserDefaults standardUserDefaults];
    
    if([preferences objectForKey:FAVORIS_KEY] == nil)
    {
        self.favoris = [[NSMutableDictionary alloc] init];
    }
    else
    {
        self.favoris = [[NSMutableDictionary alloc] init];
        
        [self.favoris addEntriesFromDictionary:[preferences objectForKey:FAVORIS_KEY]];
    }
}

-(void)ajouter:(NSString*)annonceId type:(NSString*)type
{
    [self.favoris setObject:type forKey:annonceId];
    
    [self sauvegarder];
}

-(NSString *)recuperer:(NSString *)annonceId{
    return  [self.favoris objectForKey:annonceId];
}

-(BOOL)estFavoris:(NSString*)annonceId
{
    return [self.favoris objectForKey:annonceId] != nil;
}
-(void)supprimerFavoris:(NSString*)annonceId
{
    [self.favoris removeObjectForKey:annonceId];
    
    [self sauvegarder];
}

@end
